//
//  YYTableView.m
//  YYKit-Study
//
//  Created by dev on 17/1/3.
//  Copyright © 2017年 zsw. All rights reserved.
//

#import "YYTableView.h"

@implementation YYTableView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        // delaysContentTouches, 若该属性值为YES，滚动视图会延迟处理下压手势直到可以确定该操作的意图是否是滚动。若值为NO，滚动视图会立即调用touchesShoudBegin:withEvent:inContentView 去询问该怎样处理
        self.delaysContentTouches = NO;
        self.canCancelContentTouches = YES;
//        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        // Remove touch delay (since iOS 8)
        UIView *wrapView = self.subviews.firstObject;
        // UITableViewWrapperView
        if (wrapView && [NSStringFromClass(wrapView.class) hasSuffix:@"WrapperView"]) {
            
            for (UIGestureRecognizer *gesture in wrapView.gestureRecognizers) {
                // UIScrollViewDelayedTouchesBeganGestureRecognizer
                if ([NSStringFromClass(gesture.class) containsString:@"DelayedTouchesBegan"]) {
                    gesture.enabled = NO;
                    break;
                }
            }
        }
    }
    return self;
}

/*
 scrollView 滑动时会调用这个方法, 去询问事件的分发
 默认情况, view是UIControl时, 返回NO, 即不取消向子视图分发事件,
          view不是UIControl时, 返回YES, 即取消向子视图分发事件, 
 我们想要的情况是: 可以在touch到UIControl上并滑动时不触发UIButton而让UIScrollView自己滑动, 可以像下面这样处理(配合delaysContentTouches = NO, canCancelContentTouches = YES)
**/
- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
    
    if ([view isKindOfClass:[UIControl class]]) {
        // scrollView滑动时, 触摸点在UIControl上时, 返回YES,取消事件向view及其子视图传递(例如点到了UIButton上去滑动scrollView,UIButton的事件不响应)
        return YES;
    }
    
    return [self touchesShouldCancelInContentView:view];
}

@end
